/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers;
import play.*;
import play.mvc.*;
import play.data.*;
import static play.data.Form.*;
import play.db.jpa.*;
import views.html.*;

import javax.persistence.*;

import play.data.format.*;
import play.data.validation.*;

import models.*;


public class Sorteos extends Controller {
    
    public static Result blank() {
        return ok(sorteos.render());
    }

    @Transactional(readOnly=true)
    public static Result newSorteo() {
        Form<Sorteo> sorteoForm = form(Sorteo.class);
        return ok(createFormSorteo.render(sorteoForm));
    }

    @Transactional(readOnly=true)
    public static Result list(){
        return ok(listSorteos.render(Sorteo.page()));
    }


@Transactional
public static Result save() {
    Form<Sorteo> sorteoForm = form(Sorteo.class).bindFromRequest();
    System.out.println("FORMULARIO de SORTEO: " + sorteoForm);
    if(sorteoForm.hasErrors()) {
        return badRequest(createFormSorteo.render(sorteoForm));
    }
    sorteoForm.get().save();
    flash("success",  "Sorteo, "  +  sorteoForm.get().nomSorteo +  "  ha  sido creado.");
    return ok(sorteos.render());
}

   @Transactional(readOnly=true)
       public static Result edit(Integer codSorteo) {
        Form<Sorteo> sorteosForm = form(Sorteo.class).fill(
            Sorteo.findById(codSorteo)
        );
        return ok(editFormSorteo.render(codSorteo,sorteosForm));
        }
      
       @Transactional
       public static Result update(Integer codSorteo) {
        Form<Sorteo> sorteosForm = form(Sorteo.class).bindFromRequest();
        if(sorteosForm.hasErrors()) {
            return badRequest(editFormSorteo.render(codSorteo, sorteosForm));
        }
        sorteosForm.get().update(codSorteo);
        flash("success", "Sorteo " + sorteosForm.get().codSorteo+ " ha sido actualizado");
        return redirect("/sorteos/list");
        
        }
      
        @Transactional
    public static Result delete(Integer codSorteo) {
        Sorteo.findById(codSorteo).delete();
        flash("success", "Sorteo Eliminado");
         return redirect("/sorteos/list");
    }
    
    
    
}